package cn.sfinger.huifu.p2p.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.sfinger.huifu.p2p.base.AppConfig;
import cn.sfinger.huifu.p2p.base.HuifuConfig;
import cn.sfinger.huifu.p2p.base.HuifuConstant;
import cn.sfinger.huifu.p2p.base.HuifuException;
import cn.sfinger.huifu.p2p.integration.HuifuIntegration;
import cn.sfinger.huifu.p2p.util.StringUtil;

/**
 * 接口模拟入口
 * 
 * @author michael.wang.54@gmail.com
 * @version 1.0.0
 * @date 2015年12月10日
 */
@Controller
@RequestMapping(value="")
public class IndexController {
	private static final Logger logger= LoggerFactory.getLogger(IndexController.class);
	
	@Autowired
	private HuifuConfig huifuConfig;
	@Autowired
	private HuifuIntegration huifuIntegration;
	@Autowired
	private AppConfig appConfig;
	
	@RequestMapping(value={"/","/index"})
	public String indexAction(){
		return "index";
	}
	
	@RequestMapping(value="/service")
	public ModelAndView serviceAction(HttpServletRequest request){

		logger.info("[request info] {}", request.getParameterMap());
		ModelAndView mv= new ModelAndView();
		String cmdId= request.getParameter("CmdId");
		JSONObject result= null;
		// 组装参数列表
		Map<String,Object> data= new HashMap<String, Object>();
		data.put("location", huifuConfig.getLocation());
		
		try{
			if(StringUtil.isBlank(cmdId)){
				logger.error("CmdId is lost! [request]:{}", JSON.toJSONString(request.getParameterMap()));
				mv.setViewName("redirect:/502");
			}else if(cmdId.equals(HuifuConstant.CMDID_QUERY_ACCTS)){
				// 商户子账户查询
				result= huifuIntegration.querySubAccounts();
				logger.info("[汇付请求应答数据] {}", result);
				data.put("title", "商户子账户查询");
				data.put("data", result.toJSONString());
				mv.setViewName("show_data");
			}else if(cmdId.equals(HuifuConstant.CMDID_USER_REGISTER)){
				// 用户注册
				data.put("params", this.huifuIntegration.userRegister(
						appConfig.getLocation()+appConfig.getNoticeAddr(),	//request.getParameter("BgRetUrl"), 
						appConfig.getLocation()+appConfig.getRedirectAddr(),  //request.getParameter("RetUrl"), 
						request.getParameter("UsrId"), 
						request.getParameter("UsrName"), 
						request.getParameter("IdType"), 
						request.getParameter("IdNo"), 
						request.getParameter("UsrMp"), 
						request.getParameter("UsrEmail"), 
						request.getParameter("MerPriv"), 
						request.getParameter("CharSet")));
				
				logger.info("[准备请求汇付的数据] {}", JSON.toJSONString(data.get("params")));
				data.put("title", "用户注册");
				mv.setViewName("post_to_huifu");
			}
			else{
				logger.error("接口方法还未实现！ CmdId：{}", cmdId);
				mv.setViewName("redirect:/502");
			}
		}catch(Exception e){
			mv.setViewName("redirect:/502");
		}
		
		mv.addAllObjects(data);		
		return mv;
	}
	
	@RequestMapping(value="/502")
	public String errorAction(){
		return "502";
	}
}
